#include "XSSL/XSSLCtx.h"
#include <iostream>
#include <thread>
#include "../../xplatform/XServerClient.h"
#include "../../xplatform/XService.h"//
#include "../../xplatform/protobuf/xmsg_type.pb.h"
#include "../../xplatform/protobuf/xmsg_com.pb.h"
#include "../../xplatform/XServiceHandle.h"
#include "../../xplatform/thread_pool/XThreadPool.h"
using namespace std;
class MySSLClient:public XServerClient{
public:
    void connectedCB()override{
        cout<<"connectCB"<<endl;
        xmsg::XLoginReq req;
        req.set_password("13356hhhhh");
        req.set_username("huxiaoyun");
        SendMsg(xmsg::MsgType::MSG_LOGIN_REQ,&req);
    }
};
class MyServiceHandle:public XServiceHandle{
public:
    void LoginReq(xmsg::XMsgHead*head,XMsg*msg){
        cout<<"LoginReq"<<endl;
        xmsg::XLoginReq req;
        if(req.ParseFromArray(msg->data,msg->recv_size)){
            cout<<"user_name:"<<req.username()<<endl;
            cout<<"password:"<<req.password()<<endl;
        }
    }
    static void RegMsgCall(){
        RegCB(xmsg::MsgType::MSG_LOGIN_REQ,(MsgCBFunc)&MyServiceHandle::LoginReq);
    }
    void connectedCB() override{
        cout<<"ConnectedCB"<<endl;
    }
};
class MySSLServer:public XService{
public:
    XServiceHandle *CreateServiceHandle()override{
        return new MyServiceHandle();
    }
};
int main(int argc, char** argv){
    std::cout<<"Hello World"<<std::endl;
    MyServiceHandle::RegMsgCall();
    MySSLServer server;
    XSSLCtx ctx;
    if(!ctx.InitServer("server.crt","server.key","server.crt"))
    {
        cerr << "InitServer err"<<endl;
        return -1;
    }
    server.setSSLCtx(&ctx);
    server.set_server_port_(20300);
    server.Start();
    this_thread::sleep_for(std::chrono::seconds(2));

    MySSLClient client;
    client.setServerIP("127.0.0.1");
    client.setServerPort(20300);//20000
    XSSLCtx ctx1;
    ctx1.InitClient("server.crt");
    client.setSSLCtx(&ctx1);
    client.StartConnect();
    XThreadPool::Wait();
}